import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;




public class NewGameFrame extends JFrame implements ActionListener,Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton startButton;
	private JButton loadButton;
	JComboBox potentialCB;
	JComboBox certainCB;
	JComboBox teamCB;
	
	public NewGameFrame (Color [] colorArray, int teamNumber)
	{
		this.setLayout(null);
		JLabel nameLabel = new JLabel("Number of Certain Oil Fields:");
		nameLabel.setBounds(20, 20, 200, 20);
		this.add(nameLabel);
		
		JLabel idLabel = new JLabel("Number of Potential Oil Fields:");
		idLabel.setBounds(20, 50, 200, 20);
		this.add(idLabel);
		
		JLabel colorLabel = new JLabel("Number of Playing Teams:");
		colorLabel.setBounds(20, 80, 200, 20);
		this.add(colorLabel);
		
		Integer numberArray32[] = makeArray(32);
		Integer numberArray120[] = makeArray(120);
		
		certainCB = new JComboBox(numberArray120);
		certainCB.setBounds(220,20,70,20);
		this.add(certainCB);
		
		potentialCB = new JComboBox(numberArray120);
		potentialCB.setBounds(220,50,70,20);
		this.add(potentialCB);
		
		teamCB = new JComboBox(numberArray32);
		teamCB.setBounds(220,80,70,20);
		this.add(teamCB);
		
		ImageIcon icon = new ImageIcon("img/OilFields/square_blue.png");
		ImageIcon iconScaled = scaleTo20(icon);
		OilField oilfield = new OilField(290,20,iconScaled,true);
		oilfield.setMoveable(false);
		this.add(oilfield);
		
		ImageIcon icon1 = new ImageIcon("img/OilFields/square_yellow.png");
		ImageIcon iconScaled1 = scaleTo20(icon1);
		OilField oilfield1 = new OilField(290,50,iconScaled1,true);
		oilfield1.setMoveable(false);
		this.add(oilfield1);
		
		
		
		startButton = new JButton("Start Game");
		startButton.setBounds(50, 119, 100, 30);
		this.add(startButton);
		startButton.addActionListener(this);
		
		loadButton = new JButton("Load Game");
		loadButton.setBounds(160, 119, 100, 30);
		this.add(loadButton);
		loadButton.addActionListener(this);
		
		this.setSize(380, 188);

		
	}
	
	public static Integer[] makeArray(int size)
	{
		Integer array[] = new Integer[size];
		for(int i=0; i < size; i++)
		{
			array[i] = (i+1);
		}
		return array;
	}
	
	public static void main(String[]args)
	{
		Color [] colorArray = { Color.RED, Color.BLUE, Color.BLACK, Color.WHITE };
		NewGameFrame frame = new NewGameFrame(colorArray,3);
		frame.setSize(340, 200);
	    frame.setVisible(true);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == startButton )
		{
			
			NewSwingApp inst = new NewSwingApp(certainCB.getSelectedIndex()+1,potentialCB.getSelectedIndex()+1,teamCB.getSelectedIndex()+1);
			inst.setTitle("Red Rock Oil Rig Game");
			inst.setLocationRelativeTo(null);
			this.setVisible(false);
			inst.setVisible(true);
			
			
			
		}
		if(e.getSource() == loadButton )
		{
			ObjectInputStream objectIn = null;

		    try {
				objectIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream(
				    "saved.txt")));
				
				int certainOFcount = objectIn.read();
				System.out.println(certainOFcount);
				int potentialOFcount = objectIn.read();
				System.out.println(potentialOFcount);
				int teamCount = objectIn.read();
				System.out.println(teamCount);
				int itemCount = objectIn.read();
				System.out.println(itemCount);
				ArrayList<saveItem> savedItems = new ArrayList<saveItem>();
				for(int i=0;i<itemCount;i++)
				{
					saveItem newItem = (saveItem) objectIn.readObject();
					savedItems.add(newItem);
				}
				NewSwingApp inst = new NewSwingApp(savedItems,certainOFcount,potentialOFcount,teamCount);
				inst.setTitle("Red Rock Oil Rig Game");
				inst.setLocationRelativeTo(null);
				this.setVisible(false);
				inst.setVisible(true);
				
				  objectIn.close();
		    } catch (NotSerializableException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
		    
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ClassNotFoundException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		}
		
	}
	public static ImageIcon scaleTo20 (ImageIcon img)
	{
		BufferedImage resizedImage = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(img.getImage(), 0, 0, 20, 20, null);
		g.dispose();
		g.setComposite(AlphaComposite.Src);
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		return new ImageIcon(resizedImage);
	}

}
